package com.nandi.deviceinstall.ui.activity

import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.load.model.LazyHeaders
import com.bumptech.glide.request.RequestOptions
import com.nandi.deviceinstall.R
import com.nandi.deviceinstall.base.BaseMvpActivity
import com.nandi.deviceinstall.bean.BaseObjectBean
import com.nandi.deviceinstall.bean.LoginBean
import com.nandi.deviceinstall.common.Constant
import com.nandi.deviceinstall.contract.UploadSuccessEquipmentContract
import com.nandi.deviceinstall.presenter.EquipmentStepPresenter
import com.nandi.deviceinstall.presenter.UploadSuccessEquipmentPresenter
import com.nandi.deviceinstall.util.CustomToast
import com.nandi.deviceinstall.util.FileUtils
import com.nandi.deviceinstall.util.SharedTool
import kotlinx.android.synthetic.main.activity_upload_success_equipment.*
import kotlinx.android.synthetic.main.toolbar_back_include.*
import okhttp3.ResponseBody
import java.io.File
import java.io.IOException

class UploadSuccessEquipmentActivity : BaseMvpActivity<UploadSuccessEquipmentPresenter>(), UploadSuccessEquipmentContract.View {
    override val layoutId: Int
        get() = R.layout.activity_upload_success_equipment
    private var path = ""

    override fun initView() {
        mPresenter = UploadSuccessEquipmentPresenter()
        mPresenter.attachView(this)
        pic_success.setImageResource(R.drawable.step_success)
        path = intent.extras.getString("imgPath")
        if (path.isNotEmpty()) {
            var imagePath = "${Constant.fileUrl}${path}&type=6"
            val headers = LazyHeaders.Builder().addHeader("Authorization", SharedTool.get(context,
                    Constant.TOKEN, "") as String).build()
            val requestOptions = RequestOptions().error(R.mipmap.default_image)
                    .placeholder(R.mipmap.default_image).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true)
            Glide.with(context)
                    //.asBitmap()

                    .load(GlideUrl(imagePath, headers))
                    .apply(requestOptions)
                    .into(iv)
        }
        tv_title_toolbar.text = "设备安装踩点"

        title_image.setImageResource(R.drawable.record)
        initListener()
    }

    private fun initListener() {
        title_image.setOnClickListener {
            startActivity(Intent(context, TapRecordActivity::class.java))
        }
        iv_back_toolbar.setOnClickListener {
            val intent = Intent(this@UploadSuccessEquipmentActivity, EquipmentStepActivity::class.java)
            intent.putExtra("action", "add")
            startActivity(intent)
            finish()
        }
        downLoad.setOnClickListener {
            mPresenter.requestDownPic(SharedTool.get(context, Constant.TOKEN, "") as String, path)
        }
    }

    override fun downPicSuccess(result: ResponseBody, fileName: String) {
        var bitmap: Bitmap? = null
        val bys: ByteArray
        var savePath = ""
        try {
            bys = result.bytes()
            bitmap = BitmapFactory.decodeByteArray(bys, 0, bys.size)
            try {
                FileUtils.saveImg(bitmap, Constant.IMG_PATH, fileName)
                showLongToast("下载成功,保存到${Constant.IMG_PATH + fileName}")
            } catch (e: IOException) {
                e.printStackTrace()
            }

        } catch (e: IOException) {
            e.printStackTrace()
        }
        bitmap?.recycle()
    }

    override fun showLoading() {

    }

    override fun hideLoading() {
    }

    override fun onError(msg: String, code: Int) {
        CustomToast.showToast(context, msg)
    }

    override fun onBackPressed() {
        val intent = Intent(this@UploadSuccessEquipmentActivity, EquipmentStepActivity::class.java)
        intent.putExtra("action", "add")
        startActivity(intent)
        finish()
    }
}
